package com.rabbit.producer.broker;

import com.google.common.collect.Lists;
import com.rabbit.api.Message;

import java.util.List;

/**
 * message缓存类
 * @author: 顾志杰
 */
public class MessageHolder {

    private List<Message> messages = Lists.newArrayList();

    /**
     * 线程局部变量
     */
    @SuppressWarnings({"rawtypes","unchecked"})
    public static final  ThreadLocal<MessageHolder> holder = ThreadLocal.withInitial(() -> new MessageHolder());


    public static void add(Message message){
        holder.get().messages.add(message);
    }

    public static List<Message> clear(){
        List<Message> tmp = Lists.newArrayList(holder.get().messages);
        holder.remove();
        return tmp;
    }
}
